from abc import ABC, abstractmethod
from decimal import Decimal


class Validate(ABC):
    @abstractmethod
    def validate(self, val) -> bool:
        pass
    
    @abstractmethod
    def msg_suffix(self):
        return 'xxx'


class Required(Validate):

    def validate(self, val) -> bool:
        if val is None:
            return False
        return True

    @property
    def msg_suffix(self):
        return ' is required'


class MustDecimal(Validate):
    
    def __init__(self, minimum: Decimal = 0, maxmum: Decimal = None) -> None:
        '''
        :param Decimal minimum: 最小值
        :param Decimal maxmum: 最大值
        '''
        if minimum is not None and not isinstance(minimum, Decimal):
            minimum = Decimal(str(minimum))
        if maxmum is not None and not isinstance(maxmum, Decimal):
            maxmum = Decimal(str(maxmum))
        self.minimum = minimum
        self.maxmum = maxmum

    def validate(self, val) -> bool:
        if not isinstance(val, Decimal):
            return False
        if self.minimum and val < self.minimum:
            return False
        if self.maxmum and val > self.maxmum:
            return False
        return True

    @property
    def msg_suffix(self):
        if self.minimum and self.maxmum:
            return f' 参数类型必须为Decimal, 切值范围是 [{self.minimum}, {self.maxmum}]'
        if self.maxmum:
            return f' 值必须小于等于 {self.maxmum}'
        if self.minimum:
            return f' 值必须大于等于 {self.minimum}'
        return f' 值必须能够转换为Decimal'
